package com.itheima.health.controller.mobile;

import com.aliyun.oss.ClientException;
import com.itheima.health.common.MessageConst;
import com.itheima.health.result.Result;
import com.itheima.health.util.AliyunSmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class ValidateCodeController {


    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send")
    public Result sendCode(String telephone) throws ClientException {
        AliyunSmsUtils.setNewcode();
        String code = Integer.toString(AliyunSmsUtils.getNewcode());


        try {
            //AliyunSmsUtils.sendShortMessage(AliyunSmsUtils.VALIDATE_CODE, telephone, code);
            log.info("验证码{}",code);

        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
        }
        redisTemplate.opsForValue().set("telephone",telephone,60,TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("code",code,60, TimeUnit.MINUTES);

        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);

    }

}



